<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\8 0008
 * Time: 22:23
 */
namespace app\console\controller;

use app\common\model\User;
use app\common\service\Redis;
use think\worker\Events;
use GatewayWorker\Lib\Gateway;
use think\facade\Session;
class GatePush extends Events{

    public static function onConnect($client_id)
    {
    }

    public static function onMessage($client_id, $data)
    {
        $redis = new Redis();
        $data = json_decode($data,true);

//        file_put_contents('/tmp/onMessage.txt',var_export($data,1)."\r\n",FILE_APPEND);
        $userModel = model('common/user');
        $userGroupModel = model('common/userGroup');
        switch ($data['type']){
            case 'open':
                $user_id = $data['user_id'];
                $userModel->setStatus($user_id,User::ON_LINE);
                $_SESSION['uid'] = $user_id;
                $send_data['user_id'] = $user_id;
                $send_data['user_info'] = $userModel->findUserById($user_id);
                Gateway::bindUid($client_id,$user_id);
                Gateway::sendToAll(json_encode(['type'=>'open','send_data'=>$send_data]));
                break;
            case 'sendMessage':
                $to = $data['data']['to'];
                $mine = $data['data']['mine'];
                $data['data']['mine']['content'] = htmlspecialchars($mine['content']);
                $to_status = $userModel->getUserStatus($to['id']);
                if($data['data'] != ''){
                    if($to['type'] == "friend"){
                        $key = 'rec:friend:'.getRecKeyName($mine['id'],$to['id']);
                        $redis->RPUSH($key,json_encode($data['data']));
                    }else{
                        $key = 'rec:group:'.$to['id'];
                        $redis->zAdd($key,time(),json_encode($data['data']));
                    }
                }

                if($to['type'] == 'group'){
                    $redis->set('unread_group_' . $to['id'].'_'.$mine['id'], time());
                    $userIds = $userGroupModel->getMemberIds($to['id']);
                    foreach ($userIds as $userId){
                        if($userId['user_id'] == $mine['id']){
                            continue;
                        }
                        Gateway::sendToUid($userId['user_id'], json_encode(['type'=>'receiveMessage','send_data'=>$data['data']]));
                    }
                }elseif ($to['type'] == "friend" && $to_status == -1){
                    $redis->hIncrBy('unread_friend_' . $to['id'], $mine['id'], 1);
                    $redis->hDel('unread_friend_' .$mine['id'],$to['id']);
                }else{
                    $redis->hIncrBy('unread_friend_' . $to['id'], $mine['id'], 1);
                    $redis->hDel('unread_friend_' .$mine['id'],$to['id']);
                    Gateway::sendToUid($to['id'], json_encode(['type'=>'receiveMessage','send_data'=>$data['data']]));
                }

                break;
            case 'hide':
                $user_id = $data['user_id'];
                $userModel->setStatus($user_id,User::HIDDLE);
                Gateway::sendToAll(json_encode(['type'=>'hide','send_data'=>['user_id'=>$user_id]]));
                break;
        }
    }

    public static function onClose($client_id)
    {
        $user_id = $_SESSION['uid'];
        $userModel = model('common/user');
        $userModel->setStatus($user_id,User::OUT_LINE);
        Gateway::sendToAll(json_encode(['type'=>'close','send_data'=>['user_id'=>$user_id]]));
    }
}